﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Course.Models;
using Course.Views;
using System.Windows.Forms;

namespace Course.Controllers
{
   public   class ChangePwdController
    {
        public ChangePwd View;
        public ChangePwdModel Model;
        private String userId { get; set; }
        public DataClasseDataContext context = new DataClasseDataContext();
        public ChangePwdController(ChangePwd view,string id)
        {
            Model = new ChangePwdModel();
            this.View = view;
            this.View.Controller = this;
            this.userId = id;
        }

        public String change()
        {
            MessageBox.Show("userId int controller:"+userId);
            User user = context.User.SingleOrDefault(u=>u.UserId==userId);
            if (user == null)
            {
                return "无法更改密码，查找不到您的用户信息";
            }
            else
            {
                String pwd = user.Password;
                if (pwd.CompareTo(Model.old) != 0) return "密码验证不正确！";
                else
                {
                    if (Model.newPwd.CompareTo(Model.confirm) != 0) return "密码与确认密码不一致！";
                    user.Password = Model.newPwd;
                    context.SubmitChanges();
                    return "success";
                }
            }
        }
    }
}
